﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace MengCC.Plugin.SDK
{
    public sealed class PluginContext
    {
        private string[] args;

        public PluginContext(string[] args)
        {
            this.args = args;
        }

        public Dictionary<string, string> GetParameters()
        {
            Dictionary<string, string> keyvalues = new Dictionary<string, string>();

            for (int i = 0; i < args.Length / 2 + 1; i += 2)
            {
                if (i < args.Length && (i + 1) < args.Length)
                {
                    keyvalues.Add(args[i].Trim('-'), args[i + 1]);
                }
            }
            return keyvalues;
        }
        public string Wordspace { get => System.Web.HttpUtility.UrlDecode(GetParameters()["workspace"]); }

        public string GetOptions()
        {
            return System.Web.HttpUtility.UrlDecode(GetParameters()["config"]);
        }
        public T GetOptions<T>()
        {
            return JsonConvert.DeserializeObject<T>(GetOptions());
        }

        public void Write(string msg)
        {
            Console.WriteLine(msg);
        }
        public string Read()
        {
            return Console.ReadLine();
        }
    }
}
